package com.hellpro.lolbox.video.model;

import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.video.bean.ClassifySecondInfoBean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/25
 * 备注:
 */

public class ClassifySecondLvAdapter extends BaseAdapter {

    private List<ClassifySecondInfoBean> mList;

    public ClassifySecondLvAdapter() {
        mList = new ArrayList<>();
    }

    public void setData(List<ClassifySecondInfoBean> data) {
        mList.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_classify_second_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ClassifySecondInfoBean bean = mList.get(position);

        holder.videoClassifyItemIvShow.setImageURI(Uri.parse(bean.getCover_url()));
        holder.videoClassifyItemTvTitle.setText(bean.getTitle());

        BigDecimal b = new BigDecimal(Double.parseDouble(String.valueOf(bean.getVideo_length()/60)));
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        holder.videoClassifyItemTvLengh.setText(f1+"");
        String upload_time = bean.getUpload_time();
        String substring = upload_time.substring(5, 10);
        holder.videoClassifyItemTvTime.setText(substring);
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.video_classify_item_ivShow)
        SimpleDraweeView videoClassifyItemIvShow;
        @BindView(R.id.video_classify_item_tvTitle)
        TextView videoClassifyItemTvTitle;
        @BindView(R.id.video_classify_item_tvLengh)
        TextView videoClassifyItemTvLengh;
        @BindView(R.id.video_classify_item_tvTime)
        TextView videoClassifyItemTvTime;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
